﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PodcastGenerator;

namespace PodcastGeneratorTests
{
    [TestClass]
    public class SermonBuilderTest
    {
        private Scraper.DataCell _date;
        private SermonBuilder _sermonBuilder;
        private Scraper.DataCell _downloadUri;
        private Scraper.DataCell _size;
        private Scraper.DataCell _pastor;
        private Scraper.DataCell _ref;
        private Scraper.DataCell _title;
        private Scraper.DataCell _badDate;
        private DateTime _dateAsDateTime;

        [TestInitialize]
        public void Init()
        {
            _badDate = new Scraper.DataCell(0, "36 January 2013", "36 January 2013");
            _date = new Scraper.DataCell(0, "06 January 2013", "06 January 2013");
            _dateAsDateTime = new DateTime(2013, 1, 6);
            _title = new Scraper.DataCell(0, "The Sovereignty of God", "The Sovereignty of God");
            _pastor = new Scraper.DataCell(0, "Dr Arnold Mol", "Dr Arnold Mol");
            _ref = new Scraper.DataCell(0, "", "");
            _downloadUri = new Scraper.DataCell(0, "", @"<a href=""http://sermons.waterkloofbaptist.org.za/sermons/20130106am.mp3"" target=""_blank""><img src=""./Waterkloof Baptist Church_files/note.jpg"" width=""18"" height=""24""></a>");
            _size = new Scraper.DataCell(0, "14 MB", "14 MB");

            _sermonBuilder = new SermonBuilder();
        }

        [TestMethod]
        public void CreateSermon()
        {
            var sermon = _sermonBuilder.Build(new Scraper.DataCell[] {_date, _title, _pastor, _ref, _downloadUri, _size});
     
            Assert.AreEqual(sermon.Date, _dateAsDateTime);
            Assert.AreEqual(sermon.DownloadUri.ToString(), "http://sermons.waterkloofbaptist.org.za/sermons/20130106am.mp3");
            Assert.AreEqual(sermon.FileSize, _size.InnerText);
            Assert.AreEqual(sermon.Pastor, _pastor.InnerText);
            Assert.AreEqual(sermon.Ref, _ref.InnerText);
            Assert.AreEqual(sermon.Title, _title.InnerText);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ThrowExceptionIfInvalidArray()
        {
            var sermon = _sermonBuilder.Build(new Scraper.DataCell[] { _date });
        }

        [TestMethod]
        public void SetDateToMinimumOnBadDate()
        {
            var sermon = _sermonBuilder.Build(new Scraper.DataCell[] { _badDate, _title, _pastor, _ref, _downloadUri, _size });
            Assert.AreEqual(DateTime.MinValue, sermon.Date);
        }
        
    }
}